package top.kaoshanji.p2chapters.mapper;

import org.apache.ibatis.session.SqlSession;
import org.junit.Assert;
import org.junit.Test;
import top.kaoshanji.p2chapters.model.SysPrivilege;
import top.kaoshanji.p2chapters.model.SysRole;
import top.kaoshanji.p2chapters.model.SysRole1;
import top.kaoshanji.p2chapters.type.Enabled;

import java.util.List;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/4/15 17:50
 */
public class RoleMapperTest extends BaseMapperTest {

    /**
     * 代码清单test-p2chapters-149页a：测试获取所有角色和对应的权限信息
     */
    @Test
    public void testSelectAllRoleAndPrivileges() {
        SqlSession sqlSession = getSqlSession();
        try {
            RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
            List<SysRole> roleList = roleMapper.selectAllRoleAndPrivileges();
            for (SysRole role: roleList) {
                String roleName = role.getRoleName();
                System.out.println("角色名：" + roleName);
                for (SysPrivilege privilege: role.getPrivilegeList()) {
                    System.out.println("权限名：" + roleName + "-" + privilege.getPrivilegeName());
                }
            }
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 代码清单test-p2chapters-157页a：测试根据用户id获取用户的角色信息
     */
    @Test
    public void testSelectRoleByUserIdChoose() {
        SqlSession sqlSession = getSqlSession();
        try {
            RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);

            // 由于数据库数据 enable 都是 1 ，所以给启用一个角色的 enable 赋值为 0
            SysRole role = roleMapper.selectById(2L);
            role.setEnabled(0);
            roleMapper.updateById(role);

            // 获取用户 1 的角色
            List<SysRole> roleList = roleMapper.selectRoleByUserIdChoose(1L);
            for (SysRole r: roleList) {
                System.out.println("角色名：" + r.getRoleName());
                if (r.getId().equals(1L)) {
                    // 第一个角色存在权限信息
                    Assert.assertNotNull(r.getPrivilegeList());
                } else if (r.getId().equals(2L)) {
                    // 第二个角色的权限为null
                    Assert.assertNull(r.getPrivilegeList());
                    continue;
                }

                for (SysPrivilege privilege: r.getPrivilegeList()) {
                    System.out.println("权限名：" + privilege.getPrivilegeName());
                }
            }
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 代码清单test-p2chapters-175页a：测试自定义的类型处理器
     */
    @Test
    public void testTypeHandlerEnum() {
        SqlSession sqlSession = getSqlSession();
        try {
            RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
            // 先查询出角色，然后修改角色的 enabled 值为 disabled
            SysRole1 role1 = roleMapper.selectById1(2L);
            Assert.assertEquals(Enabled.enabled, role1.getEnabled());

            role1.setEnabled(Enabled.disabled);
            roleMapper.updateById1(role1);

        } finally {
            sqlSession.rollback();
            sqlSession.close();
        }
    }

}
